package com.xbongbong.sys.model.impl;

import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.sys.domain.dao.UserConfigDao;
import com.xbongbong.sys.domain.entity.UserConfigEntity;
import com.xbongbong.sys.model.UserConfigModel;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

/**
 * @author: wufeng
 * @date: 2018/8/30 9:44
 * @desrcption: 用户配置表实现类
 */
@Service("userConfigModel")
public class UserConfigModelImpl implements UserConfigModel {

    private static final String COLUMNS = "id, corpid, user_id, config_name,config_value, config_alias, add_time, update_time, del";

    @Resource
    private UserConfigDao userConfigDao;

    @Override
    public Integer insert(UserConfigEntity entity) {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return userConfigDao.insert(entity);
    }

    @Override
    public void insertBatch(List<UserConfigEntity> userConfigList) {
        if (CollectionUtils.isEmpty(userConfigList)) {
            return;
        }
        userConfigDao.insertBatch(userConfigList);
    }

    @Override
    public Integer update(UserConfigEntity entity) {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        return userConfigDao.update(entity);
    }

    @Override
    public void updateBatch(List<UserConfigEntity> userConfigList ,String corpid) {
        userConfigDao.updateBatch(userConfigList, corpid);
    }

    @Override
    public UserConfigEntity getByKey(Long key, String corpid) {
        return userConfigDao.getByKey(key, corpid);
    }

    @Override
    public UserConfigEntity getByUserId(String userId, String corpid, String configAlias) {
        return userConfigDao.getByUserId(userId, corpid, configAlias);
    }

    @Override
    public List<UserConfigEntity> list(Map<String, Object> params) {
        params.put("columns", new StringBuffer(COLUMNS));
        return userConfigDao.findEntitys(params);
    }

    @Override
    public void save(String corpid, String userId, String alias, String name, String lang) {
        UserConfigEntity userConfigEntity = userConfigDao.getByUserId(userId, corpid, alias);
        if (Objects.isNull(userConfigEntity)) {
            userConfigEntity = new UserConfigEntity(corpid, userId, name, alias, lang);
            insert(userConfigEntity);
        } else {
            userConfigEntity.setConfigValue(lang);
            update(userConfigEntity);
        }
    }

    /**
     * 获取用户配置表中的最大id
     *
     * @param param
     * @return
     */
    @Override
    public Long getMaxId(Map<String, Object> param) {
        return userConfigDao.getMaxId(param);
    }

    /**
     * 批量更新用户配置表的信息
     *
     * @param updateList
     * @return
     */
    @Override
    public int updateBatchAll(List<UserConfigEntity> updateList) {
        return userConfigDao.updateBatchAll(updateList);
    }
}
